import axios from "axios";
import "reflect-metadata";
import type { AxiosInstance } from "axios";
import { UserApi, userApi } from "@/api/modules/user";

const Base = (base: string) => {
  const fn: ClassDecorator = (target) => {
    Reflect.defineMetadata("base", base, target.prototype);
  };
  return fn;
};

function Get(url: string) {
  const fn: MethodDecorator = (target, key, descriptor: PropertyDescriptor) => {
    Reflect.defineMetadata("url", url, target, key);
    Reflect.defineMetadata("method", "GET", target, key);
  };
  return fn;
}

class Request {
  http: AxiosInstance;
  constructor() {
    this.http = axios;
  }
  send(methodName: string, ...args: any[]) {
    const base = Reflect.getMetadata("base", this);
    const url = Reflect.getMetadata("url", this, methodName);
    const method = Reflect.getMetadata("method", this, methodName) || "GET";
    console.log("发送请求", { base, url, method, args });
    return axios({
      url: `${base || ""}${url || ""}`,
      method,
      ...args,
    });
  }
}

@Base("https://api.apiopen.top/api")
class TestApi {
  private readonly http: Request;
  constructor() {
    this.http = new Request();
  }
  @Get("/getHaoKanVideo")
  getUserList(params: { page: number; size: number }) {
    this.http.send("getUserList", { params });
    // return this.send.call(this, "getUserList", { params });
  }

  @Get("/1个")
  getUserBy() {
    // this.send.bind(this, "getUserBy");
  }
}

const testApi = new TestApi();
// const req = new Request()

export const useIndex = () => {
    const onClick = () => {
      userApi.getList({ page: 0, size: 10 });
    // testApi.getUserList({ page: 0, size: 10 });
    userApi.cancelRequest()
    setTimeout(() => {
        
    }, 20);
    // const a = new Request()
    // a.send()
  };

  const onClick3 = () => {
    // testApi.getUserBy();
  };

  return { onClick, onClick3 };
};
